$(function() {
	var validCode = false;
	var code = null;
	//验证码输入框
	$('.proveNum').focus(function(){
		var newImg = $('<img class="lockImg" src="/Public/Product/img/lock_pur.png"/>');
		var oldImg = $(".lockImg");
		oldImg.remove();
		$('.lock').append(newImg);
	}).blur(function(){
		var newImg = $('<img class="lockImg" src="/Public/Product/img/lock.png"/>');
		var oldImg = $(".lockImg");
		oldImg.remove();
		$('.lock').append(newImg);
	});
	//邀请码输入框
	$('.inviteNum').focus(function(){
		var newImg = $('<img class="personImg" src="/Public/Product/img/per_pur.png"/>');
		var oldImg = $(".personImg");
		oldImg.remove();
		$('.person').append(newImg);
	}).blur(function(){
		var newImg = $('<img class="personImg" src="/Public/Product/img/perSmall.png"/>');
		var oldImg = $(".personImg");
		oldImg.remove();
		$('.person').append(newImg);
	});
	//电话号码输入框
	$('.telNum').focus(function(){
		var newImg = $('<img class="telImg" src="/Public/Product/img/tel_pur.png"/>');
		var oldImg = $(".telImg");
		oldImg.remove();
		$('.tel').append(newImg);
	}).blur(function() {
		//首先换图片
		var newImg = $('<img class="telImg" src="/Public/Product/img/tel.png"/>');
		var oldImg = $(".telImg");
		oldImg.remove();
		$('.tel').append(newImg);
		
		var inputVal = $(this).parents('.bind_content').find('input[type=hidden]').val();
		console.log(inputVal);
		//获取电话号码
		var telNum = $(this).val();
		if(telNum.length == 0) {
			$(".errAlert").text("请输入正确的手机号...").addClass("AlertShow");
	        setTimeout(function(){
	        	$(".errAlert").removeClass("AlertShow");
	        },4000);
		} else {
			//获取短信验证码
			if(isTel(telNum)) {
				if (validCode) {
					$(".errAlert").text("请稍后再试...").addClass("AlertShow");
			        setTimeout(function(){
			        	$(".errAlert").removeClass("AlertShow");
			        },4000);
				} else{
					$('.getNum').bind("click", function() {
						console.log(telNum);
						$.ajax({
							type: 'post',
							url: "../Xinxi/Verification_Code",
							data:{
								tel:telNum,
								px:inputVal
							},
							dataType: "json",
							success: function(data) {
								console.log(data);
								if(data.code == 1) {
									code = data.msg;
									$(".errAlert").text("已发送，请注意查收").addClass("AlertShow");
							        setTimeout(function(){
							        	$(".errAlert").removeClass("AlertShow");
							        },4000);
									var time = 60;
				                    var T = setInterval(function () {
				                        time--;
				                        $('.getNum').text(time + "秒");
				                        if (time == 0) {
				                            clearInterval(T);
				                        	$('.getNum').html("重新获取");
				                        	$('.getNum').attr('disabled',false);
				                        }else{
				                        	$('.getNum').attr('disabled',true);
				                        }
				                    },1000);
								} else {
									$(".errAlert").text("验证码发送失败...").addClass("AlertShow");
									$('.getNum').attr("disabled",false);
							        setTimeout(function(){
							        	$(".errAlert").removeClass("AlertShow");
							        },4000);
								}
							},
							error: function(err) {
								console.log(err);
							}
						});//ajax结束
					});
				}
			} else {
				$(".errAlert").text("请输入正确的手机号...").addClass("AlertShow");
		        setTimeout(function(){
		        	$(".errAlert").removeClass("AlertShow");
		        },4000);
			}
		}
	});
	//点击确定按钮
	$('.commit').bind('click',function(){
		var telNum = $('.telNum').val();
		var proveNum = $('.proveNum').val();	//短信验证码
		var inviteNum = $('.inviteNum').val();	//邀请码
		if (code == proveNum) {
			code = null;
			$.ajax({
				type:"post",
				url:"really",
				dataType:'json',
				data:{
					tel:telNum
				},
				success:function(res){
					if(res.code == "1"){
						$.ajax({
							type:"post",
							url:"user_save",
							dataType:'json',
							data:{
								tel:telNum,
								yzm:proveNum,
								code:inviteNum
							},
							success:function(res){
								console.log(res);
								console.log('您已经注册过了');
								$(".errAlert").text("您已经注册过了").addClass("AlertShow");
						        setTimeout(function(){
						        	$(".errAlert").removeClass("AlertShow");
						        },2000);
						        setTimeout(function(){
						        	window.location.href = 'http://a.91jfk.com/Product/Shopindex/index.html';
						        },2500);
							},
							error:function(err){
								console.log(err);
							}
						});
					}else if(res.code == "2"){
						$.ajax({
							type:"post",
							url:"user_add",
							dataType:'json',
							data:{
								tel:telNum,
								yzm:proveNum,
								code:inviteNum
							},
							success:function(res){
								console.log(res);
								console.log('注册成功');
								$(".errAlert").text("注册成功").addClass("AlertShow");
						        setTimeout(function(){
						        	$(".errAlert").removeClass("AlertShow");
						        },2000);
						        setTimeout(function(){
						        	window.location.href = 'http://a.91jfk.com/Product/Shopindex/index.html';
						        },2500);
							},
							error:function(err){
								console.log(err);
							}
						});
					}else{
						console.log("出错啦...");
					}
				},
				error:function(err){
					console.log(err);
				}
			});
		} else{
			$(".errAlert").text("邀请码输入错误...").addClass("AlertShow");
	        setTimeout(function(){
	        	$(".errAlert").removeClass("AlertShow");
	        },4000);
		}
	});
	//验证手机号
	function isTel(val) {
		var pattern = /(^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$)|(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/;
		if(pattern.test(val)) {
			return true;
		} else {
			return false;
		}
	}
	//随机生成8位数字
	function MathRand() { 
		var Num = ""; 
		for(var i = 0;i < 8;i++) { 
			Num += Math.floor(Math.random() * 10); 
		}
		return Num;
	}
});